package assets

import (
	"context"

	"gitee.com/linxing_3/sr-core/config/source"
	"gitee.com/linxing_3/sr-core/domain"
	"gitee.com/mkwchecking/sr-admin/apps/assets/adapter/http"
	"gitee.com/mkwchecking/sr-admin/apps/assets/conf"
	"gitee.com/mkwchecking/sr-admin/apps/assets/entity"
	"gitee.com/mkwchecking/sr-admin/pkg/plugins/pdatabase"
)

var _ domain.IDomain = (*AssetDomain)(nil)

func NewAssetDomain() domain.IDomain {
	return &AssetDomain{}
}

type AssetDomain struct {
}

// Install implements domain.IDomain.
func (a *AssetDomain) Install(ctx context.Context, src source.ISource) error {
	var err error
	conf.SetupConfig(src)

	if err = http.InitHttp(ctx); err != nil {
		return err
	}

	return err
}

// OnSourceChange implements domain.IDomain.
func (a *AssetDomain) BeforeStart(src source.ISource) error {

	if err := entity.AutoMigrate(pdatabase.MustGetDatabase()); err != nil {
		return err
	}

	return nil
}

// Start implements domain.IDomain.
func (a *AssetDomain) Start(ctx context.Context) error {
	/* return http.OnStart(ctx) */
	return nil
}

// Stop implements domain.IDomain.
func (a *AssetDomain) Stop(ctx context.Context) error {
	return nil
}
